 package cn.jupiter.db;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DbMetadata {
    private DatabaseMetaData dbMetaData = null;   
    
    private Connection con = null;   
    /**  
     * 获得表或视图中的所有列信息  
     */  
    public void getTableColumns(String schemaName, String tableName)   
    {   
        try  
        {   
            ResultSet rs = dbMetaData.getColumns(null, schemaName, tableName, "%");   
            while (rs.next())   
            {   
                // table catalog (may be null)   
                String tableCat = rs.getString("TABLE_CAT");   
                // table schema (may be null)   
                String tableSchemaName = rs.getString("TABLE_SCHEM");   
                // table name   
                String tableName_ = rs.getString("TABLE_NAME");   
                // column name   
                String columnName = rs.getString("COLUMN_NAME");   
                // SQL type from java.sql.Types   
                int dataType = rs.getInt("DATA_TYPE");   
                // Data source dependent type name, for a UDT the type name is   
                // fully qualified   
                String dataTypeName = rs.getString("TYPE_NAME");   
                // table schema (may be null)   
                int columnSize = rs.getInt("COLUMN_SIZE");   
                // the number of fractional digits. Null is returned for data   
                // types where DECIMAL_DIGITS is not applicable.   
                int decimalDigits = rs.getInt("DECIMAL_DIGITS");   
                // Radix (typically either 10 or 2)   
                int numPrecRadix = rs.getInt("NUM_PREC_RADIX");   
                // is NULL allowed.   
                int nullAble = rs.getInt("NULLABLE");   
                // comment describing column (may be null)   
                String remarks = rs.getString("REMARKS");   
                // default value for the column, which should be interpreted as   
                // a string when the value is enclosed in single quotes (may be   
                // null)   
                String columnDef = rs.getString("COLUMN_DEF");   
                //                 
                int sqlDataType = rs.getInt("SQL_DATA_TYPE");   
                //                 
                int sqlDatetimeSub = rs.getInt("SQL_DATETIME_SUB");   
                // for char types the maximum number of bytes in the column   
                int charOctetLength = rs.getInt("CHAR_OCTET_LENGTH");   
                // index of column in table (starting at 1)   
                int ordinalPosition = rs.getInt("ORDINAL_POSITION");   
                // ISO rules are used to determine the nullability for a column.   
                // YES --- if the parameter can include NULLs;   
                // NO --- if the parameter cannot include NULLs   
                // empty string --- if the nullability for the parameter is   
                // unknown   
                String isNullAble = rs.getString("IS_NULLABLE");   
                // Indicates whether this column is auto incremented   
                // YES --- if the column is auto incremented   
                // NO --- if the column is not auto incremented   
                // empty string --- if it cannot be determined whether the   
                // column is auto incremented parameter is unknown   
                String isAutoincrement = rs.getString("IS_AUTOINCREMENT");   
                System.out.println(tableCat + "-" + tableSchemaName + "-" + tableName_ + "-" + columnName + "-"  
                        + dataType + "-" + dataTypeName + "-" + columnSize + "-" + decimalDigits + "-" + numPrecRadix   
                        + "-" + nullAble + "-" + remarks + "-" + columnDef + "-" + sqlDataType + "-" + sqlDatetimeSub   
                        + charOctetLength + "-" + ordinalPosition + "-" + isNullAble + "-" + isAutoincrement + "-");   
            }   
        } catch (SQLException e)   
        {   
            // TODO: handle SQLException   
            e.printStackTrace();   
        }   
    }   

    /**  
     * 获得该用户下面的所有表  
     */  
    public void getAllTableList(String schemaName)   
    {   
        try  
        {   
            // table type. Typical types are "TABLE", "VIEW", "SYSTEM   
            // TABLE", "GLOBAL TEMPORARY", "LOCAL TEMPORARY", "ALIAS",   
            // "SYNONYM".   
            String[] types =   
            { "TABLE" };   
            ResultSet rs = dbMetaData.getTables(null, schemaName, "%", types);   
            while (rs.next())   
            {   
                String tableName = rs.getString("TABLE_NAME");   
                // table type. Typical types are "TABLE", "VIEW", "SYSTEM   
                // TABLE", "GLOBAL TEMPORARY", "LOCAL TEMPORARY", "ALIAS",   
                // "SYNONYM".   
                String tableType = rs.getString("TABLE_TYPE");   
                // explanatory comment on the table   
                String remarks = rs.getString("REMARKS");   
                System.out.println(tableName + "-" + tableType + "-" + remarks);   
            }   
        } catch (SQLException e)   
        {   
            // TODO: handle SQLException   
            e.printStackTrace();   
        }   
    }   

	public DatabaseMetaData getDbMetaData() {
		return dbMetaData;
	}

	public void setDbMetaData(DatabaseMetaData dbMetaData) {
		this.dbMetaData = dbMetaData;
	}

	public Connection getCon() {
		return con;
	}

	public void setCon(Connection con) {
		this.con = con;
	}

	public static void main(String[] args) {
		Connection con = null;
		String url = "jdbc:mysql://localhost:3306/simpleJupiter";
		String driver = "com.mysql.jdbc.Driver";
		Statement stmt = null;
		DbMetadata m=new DbMetadata();
		try {
			Class.forName(driver);
			try {
				con = DriverManager.getConnection(url, "root", "929929");
				stmt = con.createStatement();
				//DatabaseMetaData m=con.getMetaData();
				m.setCon(con);
				m.setDbMetaData(con.getMetaData());
				m.getTableColumns("simpleJupiter","users");
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		

	}
}
